package com.csnf.mall.controllermvc;

import com.csnf.mall.entity.ProductOrder;
import com.csnf.mall.servermvc.ProductOrderService;
import com.csnf.mall.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台管理-订单页
 */
@Controller
public class OrderController {
    @Autowired
    private ProductOrderService productOrderService;

    @RequestMapping(value = "admin/order", method = RequestMethod.GET)
    public ModelAndView goToPage(ModelAndView modelAndView,
                                 @RequestParam(defaultValue = "0") Integer index,
                                 @RequestParam(defaultValue = "10") Integer count) {
        // 创建 PageUtil 对象
        PageUtil pageUtil = new PageUtil(index, count);

        // 查询订单列表
        List<ProductOrder> productOrderList = productOrderService.getList(
                null, // 查询条件
                null, // 订单状态数组
                "productOrder_id desc", // 排序字段
                pageUtil
        );

        // 查询订单总数
        Integer total = productOrderService.getTotal(
                null, // 查询条件
                null  // 订单状态数组
        );

        // 设置 PageUtil 的总记录数
        pageUtil.setTotal(total);

        // 计算总页数
        int totalPages = (int) Math.ceil((double) total / count);
        modelAndView.addObject("totalPages", totalPages);
        modelAndView.addObject("currentPage", index + 1); // 当前页码从1开始

        // 将数据传递给前端
        modelAndView.addObject("productOrderList", productOrderList);
        modelAndView.addObject("pageUtil", pageUtil);
        modelAndView.setViewName("orderManagePage");
        return modelAndView;
    }
    @RequestMapping("/admin/orderDetails")
    public ModelAndView getOrderDetails(ModelAndView modelAndView){
        modelAndView.setViewName("orderDetails");
        return modelAndView;
    }


//
//转到后台管理-订单页-ajax
//@RequestMapping(value = "admin/order", method = RequestMethod.GET)
//public ModelAndView goToPage(ModelAndView modelAndView) {
//    PageUtil pageUtil = new PageUtil(0, 10);
//    List<ProductOrder> productOrderList =
//            productOrderService.getList(
//                    null,
//                    null,
//                    "productOrder_id desc", pageUtil);
//    modelAndView.addObject("productOrderList", productOrderList);
//    modelAndView.setViewName("orderManagePage");
//    return modelAndView;

    // 在 OrderController 类中添加以下方法，点击详情跳转到详情页面。
    @RequestMapping(value = "admin/orderDetails/{orderId}", method = RequestMethod.GET)
    public ModelAndView getOrderDetails(ModelAndView modelAndView, @PathVariable Long orderId) {
        // 使用 ProductOrderService 获取订单详细信息
        ProductOrder order = productOrderService.getOrderById(orderId);

        // 将订单信息添加到 modelAndView 中
        modelAndView.addObject("order", order);

        // 设置视图为 orderDetails
        modelAndView.setViewName("orderDetails");
        return modelAndView;
    }

}
